package com.huluobo.a2207douyindemo.shop;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.base.bean.CartBeanResult;
import com.huluobo.base.bean.CommonBeanResult;
import com.huluobo.base.bean.ShopBeanResult;
import com.huluobo.base.mvvm.BaseRepo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/9/26.
 */
public class ShopRepo extends BaseRepo {
    public void getAllGoods(int categoryId, int currentPage, int pageSize, MutableLiveData<ShopBeanResult> success,
                            MutableLiveData<String> failed) {
        apiService.getAllGoods(categoryId, currentPage, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ShopBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ShopBeanResult shopBeanResult) {
                        success.setValue(shopBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getCartList(MutableLiveData<CartBeanResult> success, MutableLiveData<String> failed) {
        apiService.getCartList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CartBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CartBeanResult cartBeanResult) {
                        success.setValue(cartBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void addToCart(RequestBody body, MutableLiveData<CommonBeanResult> success, MutableLiveData<String> failed) {
        apiService.addToCart(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommonBeanResult commonBeanResult) {
                        success.setValue(commonBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void deleteCart(RequestBody body, MutableLiveData<CommonBeanResult> success,
                           MutableLiveData<String> failed) {
        apiService.deleteCart(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommonBeanResult commonBeanResult) {
                        success.setValue(commonBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
